package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.annotaion.SystemControllerLog;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Menu;
import com.itheima.service.MenuService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author ldl.plus
 * @date 2020年04月24日  11:37
 * 菜单控制
 */
@RestController
@RequestMapping("/menu")
public class MenuController {

    @Reference//查找服务
    private MenuService menuService;

    //新增检查项
    @RequestMapping("/add")
    @SystemControllerLog("新增菜单失败")
    public Result add(@RequestBody Menu menu) {
        menuService.add(menu);
        return new Result(true, "新增菜单成功");
    }

    //检查项分页查询
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        PageResult pageResult = menuService.pageQuery(queryPageBean);
        return pageResult;
    }

    //删除检查项
    @PreAuthorize("hasAuthority('PERMISSION_DELETE')")//菜单校验
    @RequestMapping("/delete")
    @SystemControllerLog("删除菜单失败")
    public Result delete(Integer id) {
        menuService.deleteById(id);
        return new Result(true, "删除菜单成功");
    }

    //编辑检查项
    @RequestMapping("/edit")
    @SystemControllerLog("编辑菜单失败")
    public Result edit(@RequestBody Menu menu) {
        menuService.edit(menu);
        return new Result(true, "编辑菜单成功");
    }

    @RequestMapping("/findById")
    @SystemControllerLog("查询菜单失败")
    public Result findById(Integer id) {
        Menu menu = menuService.findById(id);
        return new Result(true, "查询菜单成功", menu);
    }

    @RequestMapping("/findAll")
    @SystemControllerLog("查询菜单失败")
    public Result findAll() {
        List<Menu> list = menuService.findAll();
        return new Result(true, "查询菜单成功", list);
    }
}
